package com.study.controller;

import com.study.entity.Permission;
import com.study.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     *
     */
    @RequestMapping(value = "/selectPermission_enable")
    @ResponseBody
    public List<Permission> getPermission_enable() {
        return permissionService.selectPermission_enable();
    }

    @RequestMapping(value = "/permissionManagement", method = RequestMethod.GET)
    public ModelAndView showResources() {
        ModelAndView mv = new ModelAndView();
        List<Permission> permissions = permissionService.selectPermission();
        mv.addObject("permissions", permissions);
        mv.setViewName("permission-list");
        return mv;
    }

    // 新增资源
    @RequestMapping(value = "/addPermission", method = RequestMethod.GET)
    public String addResource() {
        return "permission-add";
    }

    @RequestMapping(value = "/addPermission", method = RequestMethod.POST)
    public String addResource(Permission permission) {
        permissionService.addPermission(permission);
        return "redirect:/permission/permissionManagement";
    }

    @RequestMapping(value = "/updatePermission/{permissionId}", method = RequestMethod.GET)
    public ModelAndView updatePermission(@PathVariable(name = "permissionId") Integer permissionId) {
        ModelAndView mv = new ModelAndView();
        Permission permission = permissionService.getPermissionByResId(permissionId);
        mv.addObject("permission", permission);
        mv.setViewName("permission-update");
        return mv;
    }

    @RequestMapping(value = "/updatePermission/{permissionId}", method = RequestMethod.POST)
    public String updatePermission(Permission permission) {
        permissionService.modifyPermission(permission);
        return "redirect:/permission/permissionManagement";
    }

    @RequestMapping(value = "/deletePermission", method = RequestMethod.POST)
    public String deleteResources(Integer permissionId) {
        permissionService.deletePermission(permissionId);
        return "redirect:/permission/permissionManagement";
    }

    @RequestMapping(value = "/getParentPermission", method = RequestMethod.POST)
    @ResponseBody
    public List<Permission> getParentResourceInfo(Integer level) {
        return permissionService.getParentResourceInfo(level);
    }
}
